package com.corpcredit.webapp.action;

import java.io.Serializable;
import java.util.Date;

import com.corpcredit.Constants;
import com.corpcredit.model.Cpzlmj;
import com.corpcredit.model.Jbqk;
import com.corpcredit.service.GenericManager;

public class CpzlmjForm extends BasePage implements Serializable {
    private GenericManager<Cpzlmj, Long> cpzlmjManager;
    private Cpzlmj cpzlmj = new Cpzlmj();
    private Long id;
    private Jbqk jbqk;

    public void setJbqk( Jbqk jbqk )
    {
        this.jbqk = jbqk;
    }

    public void setCpzlmjManager(GenericManager<Cpzlmj, Long> cpzlmjManager) {
        this.cpzlmjManager = cpzlmjManager;
    }

    public Cpzlmj getCpzlmj() {
        return cpzlmj;
    }

    public void setCpzlmj(Cpzlmj cpzlmj) {
        this.cpzlmj = cpzlmj;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String delete() {
        cpzlmjManager.remove(cpzlmj.getId());
        addMessage("cpzlmj.deleted");

        return "list";
    }

    public String edit() {
        // Comparison to zero (vs. null) is required with MyFaces 1.2.2, not with previous versions
        if (id != null && id != 0) {
            cpzlmj = cpzlmjManager.get(id);
        } else {
            cpzlmj = new Cpzlmj();
        }

        return "edit";
    }

    public String save() {
        boolean isNew = (cpzlmj.getId() == null || cpzlmj.getId() == 0);
        cpzlmj.setJbqk( jbqk );
        cpzlmj.setUsername(getRequest().getRemoteUser());
        cpzlmj.setOptdate(new Date());
        cpzlmj.setOperation( (isNew) ? Constants.OPERATION_ADD : Constants.OPERATION_UPDATE );

        cpzlmjManager.save(cpzlmj);

        String key = (isNew) ? "cpzlmj.added" : "cpzlmj.updated";
        addMessage(key);

        if (isNew) {
            return "list";
        } else {
        	this.id = cpzlmj.getId();
            return "edit";
        }
    }
} 